package rad.framework.mda.properties.views;

import org.eclipse.gmf.runtime.common.ui.services.properties.PropertiesService;
import org.eclipse.jface.util.Assert;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Parameter;
import org.eclipse.uml2.uml.ParameterDirectionKind;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * @author boudyacho
 *
 */
public class ParameterSection extends AbstractPropertySection {

	private CCombo directionCC;

	private Parameter umlElement;

	private ModifyListener listener = new ModifyListener() {

		public void modifyText(ModifyEvent arg0) {
			IPropertySource properties = getPropertySource(umlElement);
			if ("in".equals(directionCC.getText())) {
				properties.setPropertyValue(UMLPackage.Literals.PARAMETER__DIRECTION,ParameterDirectionKind.IN_LITERAL);
			} else {
				properties.setPropertyValue(UMLPackage.Literals.PARAMETER__DIRECTION,ParameterDirectionKind.RETURN_LITERAL);
			}
		}
	};

	/* (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#setInput(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
	 */
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Assert.isTrue(selection instanceof IStructuredSelection);
		Object input = ((IStructuredSelection) selection).getFirstElement();
		Assert.isTrue(input instanceof Parameter);
		this.umlElement = (Parameter) input;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	public void createControls(Composite parent,TabbedPropertySheetPage aTabbedPropertySheetPage) {
		
		super.createControls(parent, aTabbedPropertySheetPage);
		Composite composite = getWidgetFactory().createFlatFormComposite(parent);
		FormData data;

		directionCC = getWidgetFactory().createCCombo(composite,SWT.BORDER);
		directionCC.add("in");
		directionCC.add("return");
		data = new FormData();
		data.left = new FormAttachment(0, STANDARD_LABEL_WIDTH);
		data.right = new FormAttachment(100, 0);
		data.top = new FormAttachment(0, ITabbedPropertyConstants.VSPACE);
		directionCC.setLayoutData(data);
		directionCC.addModifyListener(listener);

		CLabel labelLabel = getWidgetFactory().createCLabel(composite, "Direction:");
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(directionCC,-ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(directionCC, 0, SWT.CENTER);
		labelLabel.setLayoutData(data);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#refresh()
	 */
	public void refresh() {
		directionCC.removeModifyListener(listener);
		IPropertySource properties = getPropertySource(umlElement);
		Object value = properties.getPropertyValue(UMLPackage.Literals.PARAMETER__DIRECTION);
		if (value != null) {
			directionCC.setText(value.toString());
		} else {
			directionCC.setText("");
		}
		directionCC.addModifyListener(listener);
	}
	
	/**
	 * @param o
	 * @return
	 */
	protected IPropertySource getPropertySource(Object o) {
		return PropertiesService.getInstance().getPropertySource(o);		
	}

}
